import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'

// 引入 Element UI
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)

// 引入 Bootstrap 和 jQuery 相关资源
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap/dist/css/bootstrap.min.css'
import 'bootstrap/dist/js/bootstrap.bundle.min.js'
import 'bootstrap-icons/font/bootstrap-icons.css'
import 'jquery-confirm'
import 'jquery-confirm/css/jquery-confirm.css'
import '@fortawesome/fontawesome-free/css/all.min.css'

import jQuery from 'jquery'
window.$ = window.jQuery = jQuery
Vue.prototype.$ = jQuery

// 引入 Tailwind CSS
import '@/index.css'

// 引入 Chart.js
import Chart from '@/chart.js'
Chart.defaults.global = {
    responsive: true,
    maintainAspectRatio: false
}

// 引入 Tooltip
import VTooltip from 'v-tooltip'
Vue.use(VTooltip)

// 引入 WebSocket 服务
import WebSocketService from '@/utils/websocket'

// 初始化 Vue 生产提示
Vue.config.productionTip = false

// 用户信息对象
let UserInfo = {
    name: '',
    role: '',
    username: ''
}

// 异步初始化函数
async function initApp() {
    try {
        const response = await axios.get('/user/checkSession')
        if (!response.data.result) {
           // Vue.prototype.$message.error('登录超时，请重新登录！')
        } else {
            UserInfo = {
                name: response.data.name,
                role: response.data.role,
                username: response.data.username
            }
        }
    } catch (error) {
        Vue.prototype.$message.error('获取用户信息失败！')
    }

    // 初始化 WebSocket 并订阅消息
    WebSocketService.connect()
    WebSocketService.subscribe((msg) => {
        const content = msg
        if (content.toyhm === UserInfo.username) {
            Vue.prototype.$notify({
                title: `${content.fromyhxm } ,发来新消息`,
                message: content.nr,
                type: 'warning',
                duration: 5000
            })
        }
    })

    // 启动 Vue 实例
    new Vue({
        router,
        store,
        render: h => h(App)
    }).$mount('#app')
}

// 启动应用
initApp()
